import { balance, cash, profit } from './cwbbOptions.js'
import { fillInputDone } from "../../common.js"
import { xqykjzz_Month } from '../data/xqykjzz.js'

function cwbb(data) {
  let isYearReport = data.saasData.isYearReport;
  let inputData = data.saasData.data.openParams.inputData;
  let sheet = $('table.table.table_head tr td b').text().replace(/\s*/g, "")
  if (isYearReport) {
    if (sheet.includes('现金流量表')) {
      cash(inputData.data.cash, inputData.lastYearData.cash);
      fillInputDone({
        tax: 'cwbb', // 税种
        success: true, // 是否成功
        message: "财务报表-填报成功", // 文案
      })
    }
    if (sheet.includes('资产负债表')) {
      balance(inputData.data.balance);
      fillInputDone({
        tax: 'cwbb', // 税种
        success: true, // 是否成功
        message: "财务报表-填报成功", // 文案
      })
    }
    if (sheet.includes('利润表')) {
      profit(inputData.data.profit, inputData.lastYearData.profit);
      fillInputDone({
        tax: 'cwbb', // 税种
        success: true, // 是否成功
        message: "财务报表-填报成功", // 文案
      })
    }
  } else {
    try {
      fillInput(data);
      fillInputDone({
        tax: 'cwbb', // 税种
        success: true, // 是否成功
        message: "财务报表-填报成功", // 文案
      })
    } catch (e) {
      fillInputDone({
        tax: 'cwbb', // 税种
        success: false, // 是否成功
        message: e.message, // 文案
      })
    }

  }



}

function fillInput(data) {
  console.log(data)
  const { saasData, baseInfo } = data;
  const { sheet, data: inputData } = saasData;
  let fillData = inputData.openParams.inputData.data[sheet];
  fillData = cwbbTransfromData({ type: sheet }, fillData)
  for (let k in fillData) {
    let v = fillData[k];
    let input = $(`#${k}`);
    if (!input.attr('disabled')) {
      document.form1.elements[k].focus()
      input.val(`${v}`)
    }
  }
  $('body').click()
  console.log(fillData)
}

function cwbbTransfromData({ type }, data) {
  const VK = xqykjzz_Month[type];
  const obj = {};
  if (type === "balance") {
    for (const key in VK) {
      const col = VK[key].replace("h", "");
      const cell = data[col];
      const xnum = key.includes("qms")
        ? cell.colValue1 || 0.0
        : cell.colValue2 || 0.0;
      obj[key] = parseFloat(xnum);
    }
  }
  if (type === "profit" || type === "cash") {
    for (const key in VK) {
      const col = VK[key].replace("h", "");
      const cell = data[col];
      const xnum = key.includes("sns")
        ? cell.colValue2 || 0.0
        : cell.colValue1 || 0.0;
      obj[key] = parseFloat(xnum);
    }
  }
  return obj;
}
export { cwbb }